package com.along.dev.rbac.mapper;

import com.along.dev.rbac.dto.UserDTO;
import com.along.dev.rbac.entity.Menu;
import com.along.dev.rbac.qo.MenuQO;
import com.along.dev.rbac.qo.UserQO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*
 * @Author: liujinyi
 * @DateTime: 2025/9/15 17:47
 */
@Mapper
public interface UserMapper {

    @Results(id = "rbacUserMap", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "deptId", column = "dept_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "email", column = "email"),
            @Result(property = "phonenumber", column = "phonenumber"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "password", column = "password"),
            @Result(property = "status", column = "status"),
            @Result(property = "delFlag", column = "del_flag"),
            @Result(property = "loginIp", column = "login_ip"),
            @Result(property = "loginDate", column = "login_date"),
            @Result(property = "createBy", column = "create_by"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateBy", column = "update_by"),
            @Result(property = "updateTime", column = "update_time")
    })

    @Select({"<script>",
            "SELECT * FROM sys_menu WHERE 1=1",
            "<if test='qo.menuName != null and qo.menuName != \"\"'> AND menu_name LIKE CONCAT('%', #{qo.menuName}, '%')</if>",
            "<if test='qo.status != null and qo.status != \"\"'> AND status = #{qo.status}</if>",
            "<if test='qo.menuType != null and qo.menuType != \"\"'> AND menu_type = #{qo.menuType}</if>",
            "ORDER BY parent_id, order_num",
            "</script>"
    })
    Page<Menu> find(@Param("qo") MenuQO menuQO);

    @Select("SELECT * FROM sys_menu WHERE status = '0' ORDER BY parent_id, order_num")
    List<Menu> findAllVisibleMenus();

    @Insert("INSERT INTO sys_menu (" +
            "menu_name, parent_id, order_num, path, component, query, " +
            "is_frame, is_cache, menu_type, visible, status, perms, icon, " +
            "create_by, create_time, update_by, update_time, remark" +
            ") VALUES (" +
            "#{menuName}, #{parentId}, #{orderNum}, #{path}, #{component}, #{query}, " +
            "#{isFrame}, #{isCache}, #{menuType}, #{visible}, #{status}, #{perms}, #{icon}, " +
            "#{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{remark}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "menuId")
    int save(Menu menu);

    @Update("<script>" +
            "UPDATE sys_menu " +
            "<set>" +
            "<if test='menuName != null'>menu_name = #{menuName},</if>" +
            "<if test='parentId != null'>parent_id = #{parentId},</if>" +
            "<if test='orderNum != null'>order_num = #{orderNum},</if>" +
            "<if test='path != null'>path = #{path},</if>" +
            "<if test='component != null'>component = #{component},</if>" +
            "<if test='query != null'>query = #{query},</if>" +
            "<if test='isFrame != null'>is_frame = #{isFrame},</if>" +
            "<if test='isCache != null'>is_cache = #{isCache},</if>" +
            "<if test='menuType != null'>menu_type = #{menuType},</if>" +
            "<if test='visible != null'>visible = #{visible},</if>" +
            "<if test='status != null'>status = #{status},</if>" +
            "<if test='perms != null'>perms = #{perms},</if>" +
            "<if test='icon != null'>icon = #{icon},</if>" +
            "<if test='updateBy != null'>update_by = #{updateBy},</if>" +
            "update_time = #{updateTime}," +
            "<if test='remark != null'>remark = #{remark}</if>" +
            "</set>" +
            " WHERE menu_id = #{menuId}" +
            "</script>")
    int update(Menu menu);

    @Update("UPDATE sys_menu SET status = #{status} WHERE menu_id = #{menuId}")
    int updateStatus(@Param("menuId") Long menuId, @Param("status") String status);

    @Update("UPDATE sys_menu SET del_flag = '2', update_time = NOW() WHERE menu_id = #{menuId}")
    int delete(@Param("menuId") Long menuId);
}
